package com.hncboy.chatgpt.front.controller;
import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hncboy.chatgpt.front.service.KeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradePrecreateModel;

@RestController
@RequestMapping("/key")
public class KeyController {

    @Autowired
    private KeyService keyService;

    @GetMapping("/{key}/usageCount")
    public Map<String, Object> getUsageCount(@PathVariable String key) {
        try {
            int usageCount = keyService.getUsageCount(key);
            System.out.println(usageCount);
            Map<String, Object> response = new HashMap<>();
            response.put("usageCount", usageCount);
            return response;
        } catch (KeyService.InvalidKeyException | JsonProcessingException e) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "指定的key不存在");
        }
    }
}
